/**
 * 
 */
package bitManipulation;

/**
 * @author michael
Reverse bits of a given 32 bits unsigned integer.

For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).

Follow up:
If this function is called many times, how would you optimize it?

Related problem: Reverse Integer
 */
public class ReverseBits {


    // you need treat n as an unsigned value
    public int reverseBits(int n) {
        n = ((n << 1) & 0xAAAAAAAA) | ((n >> 1) & 0x55555555);
        n = ((n << 2) & 0xCCCCCCCC) | ((n >> 2) & 0x33333333);
        n = ((n << 4) & 0xF0F0F0F0) | ((n >> 4) & 0x0F0F0F0f);
        n = ((n << 8) & 0xFF00FF00) | ((n >> 8) & 0x00FF00FF);
        n = ((n << 16) & 0xFFFF0000) | ((n >> 16) & 0x0000FFFF);
    	
    	return n;
    }
	/**
	 * 
	 */
	public ReverseBits() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
